package ws.log;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Provide three methods.
 * DebugMsg  writes the debug message.
 * DBMsg  recordes the db operation message.
 * MailMsg	 recordes the mail operation message.
 * 
 * */
public class Log {

	
	public static String debugLogPath = "C:\\wslog\\debugeLog.log";
	public static String dbMiddleLayerLogPath = "C:\\wslog\\dbMiddleLayerLog\\";
	public static String mailLogPath = "C:\\wslog\\mailLog\\";
	public static String formAttrLogPath = "C:\\wslog\\FormAttrLog\\";
	
	private static void writeMsg(String msg,Exception ex,String fileName){
		try{
			FileWriter fw = null;
			PrintWriter pw = null;
			try{
				File file = new File(fileName);
				if(!file.exists()){
					file.createNewFile();
				}
				fw = new FileWriter(file,true);
				pw = new PrintWriter(fw);
				SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String nowtime=dateformat.format(new Date());
				fw.write("["+nowtime+"]"+msg+"\r\n");
				if(ex != null){
					fw.write(ex.getMessage());
					ex.printStackTrace(pw);
				}
				
			}catch(IOException ioex){
				System.out.print(ioex.getMessage());
			}finally{
				if(fw!=null)
					fw.close();
				if(pw!=null)
					pw.close();
			}
		}catch(IOException e){
			System.out.print(e.getMessage());
		}
	}
	
	public static void writeDebug(String msg,Exception ex){
			writeMsg(msg,ex,debugLogPath);
	}
	
	public static void writeDBMiddleLayerLog(String msg,Exception ex){
		Date now = new Date();
		SimpleDateFormat dateformatForFileName=new SimpleDateFormat("yyyy-MM-dd");
		String filetime=dateformatForFileName.format(now);
		String filename = dbMiddleLayerLogPath+filetime+"DBLog.log";
		writeMsg(msg,ex,filename);
	}
	
	public static void writeMailLog(String msg,Exception ex){
		Date now = new Date();
		SimpleDateFormat dateformatForFileName=new SimpleDateFormat("yyyy-MM-dd");
		String filetime=dateformatForFileName.format(new Date());
		String filename = mailLogPath+filetime+"MailLog.log";
		writeMsg(msg,ex,filename);
	}
	
	public static void writeFormAttrLog(String msg,Exception ex){
		Date now = new Date();
		SimpleDateFormat dateformatForFileName=new SimpleDateFormat("yyyy-MM-dd");
		String filetime=dateformatForFileName.format(new Date());
		String filename = formAttrLogPath+filetime+"FormAttrLog.log";
		writeMsg(msg,ex,filename);
	}
	
	
	

}
